package com.ydgk.dao;

import com.ydgk.bean.Provider;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.List;

public class ProviderDao extends BaseDao<Provider>{
    @Override
    public int save(Provider provider) {
        return this.template.update("insert into provider values(null,?,?,?,?,null,?)",provider.getPname(),provider.getPeople(),provider.getPhone(),provider.getAddress(),provider.getPmoney());
    }

    @Override
    public int updateById(Provider provider) {
        return this.template.update("update provider set pname=?,phone=?,people=?,address=? where pid=?",
                provider.getPname(),provider.getPhone(),provider.getPeople(),provider.getAddress(),provider.getPid());
    }

    @Override
    public int deleteById(int id) {
        return this.template.update("delete from provider where pid=?",id);
    }

    @Override
    public List<Provider> findAll() {
        return this.template.query("select * from provider",new BeanPropertyRowMapper<>(Provider.class));
    }

    @Override
    public Provider findById(int id) {
        return this.template.queryForObject("select * from provider where pid=?",new BeanPropertyRowMapper<>(Provider.class),id);
    }

    @Override
    public List<Provider> paging(int pageNumber) {
        return this.template.query("select * from provider order by pid desc limit ?,?",
                new BeanPropertyRowMapper<>(Provider.class),(pageNumber-1)*pageCount,pageCount);

    }

    public int updateMoney(Provider provider,int money){
        return this.template.update("update provider set pmoney=pmoney + ? where pid=?",money,provider.getPid());
    }

    public int total(){
        return this.template.queryForObject("select count(*) from provider",Integer.class);
    }


}
